package calorie.com.calorie.activity;

import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;

import com.google.gson.Gson;
import com.lxh.baselibray.dialog.AlertDialog;
import com.lxh.baselibray.mvp.MvpActivity;
import com.lxh.baselibray.util.ObjectUtils;
import com.lxh.baselibray.util.SPUtils;
import com.lxh.baselibray.util.SizeUtils;
import com.lxh.baselibray.util.ToastUtils;
import com.lxh.baselibray.view.PswText;
import com.lxh.baselibray.view.TitleView;

import butterknife.BindView;
import butterknife.OnClick;
import calorie.com.calorie.R;
import calorie.com.calorie.constant.Constant;
import calorie.com.calorie.contact.DrawCoinContact;
import calorie.com.calorie.response.DrawCoinResponce;
import calorie.com.calorie.response.UserAssets;
import calorie.com.calorie.util.StaticValue;
import retrofit2.Response;

/**
 * 提币
 * Created by ThinkPad on 2018-11-28.
 */

public class WithdrawMoneyActivity extends MvpActivity<DrawCoinContact.DrawCoinPresent> implements DrawCoinContact.IDrawCoinView {
    @BindView(R.id.tv_CurrencyType)
    TextView tv_CurrencyType;
    @BindView(R.id.tv_MoneyAddrs)
    TextView tvMoneyAddrs;
    @BindView(R.id.et_num)
    EditText etNum;
    @BindView(R.id.titleView)
    TitleView titleView;
    @BindView(R.id.tv_confirm_draw_coin)
    TextView tvConfirmDrawCoin;
    private String type = "CAL";


    @Override
    public void initData(Bundle savedInstanceState) {
        tvMoneyAddrs.setText(SPUtils.getString(Constant.PURSE_ADDRESS, null, context));

    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_wallet_withdraw_money;
    }

    @OnClick({R.id.tv_confirm_draw_coin, R.id.tv_CurrencyType})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_confirm_draw_coin:
                String numText = etNum.getText().toString();
                if (ObjectUtils.isEmpty(numText) || Double.valueOf(numText) <= 0) {
                    ToastUtils.showLongToast(context, "请输入代币数量");
                    return;
                }
                showDealDialog(Double.valueOf(numText));
                break;
            case R.id.tv_CurrencyType:
                showTypeDialog();
                break;

        }
    }


    AlertDialog dealDialog = null;

    private void showDealDialog(double tokenNum) {
        AlertDialog.Builder builder = new AlertDialog.Builder(this)
                .addDefaultAnimation()
                .setCancelable(true)
                .setContentView(R.layout.dialog_deal_pwd)
                .setWidthAndHeight(SizeUtils.dp2px(context, 279), SizeUtils.dp2px(context, 157))
                .setOnClickListener(R.id.tv_sure, v -> {
                    PswText psw = dealDialog.getView(R.id.pt_deal_pwd);
                    String dealPwd = psw.getPsw();
                    mPresent.drawCoin(context, tokenNum, dealPwd, type);
                    dealDialog.dismiss();

                }).setOnClickListener(R.id.tv_cancle, v -> dealDialog.dismiss());
        dealDialog = builder.create();
        dealDialog.show();
    }

    @Override
    public void getDrawCoinData(Response<DrawCoinResponce> response) {
        if (response.body().getCode() == Constant.SUCCESS_CODE) {
            ToastUtils.showLongToast(context, "提币成功");
            StaticValue.userAssets = new Gson().fromJson(new Gson().toJson(response.body().getData()), UserAssets.class);
            finish();
        } else {
            ToastUtils.showLongToast(context, response.body().getMsg());
        }
    }

    @Override
    protected DrawCoinContact.DrawCoinPresent createPresent() {
        return new DrawCoinContact.DrawCoinPresent();
    }

    AlertDialog typeDialog = null;

    public void showTypeDialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(context)
                .addDefaultAnimation()
                .setCancelable(true)
                .setText(R.id.tv_one, "CAL")
                .setText(R.id.tv_two, "激活券")
                .setContentView(R.layout.dialog_type_select)
                .fromBottom(true)
                .setWidthAndHeight(ViewGroup.LayoutParams.MATCH_PARENT, SizeUtils.dp2px(context, 80))
                .setOnClickListener(R.id.tv_one, v -> {
                    type = "CAL";
                    tv_CurrencyType.setText("CAL");
                    typeDialog.dismiss();
                }).setOnClickListener(R.id.tv_two, v -> {
                    type = "激活券";
                    tv_CurrencyType.setText("激活券");
                    typeDialog.dismiss();
                });
        typeDialog = builder.create();
        typeDialog.show();
    }
}
